package com.abc.xxx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.abc.xxx.dao.SysConfigMapper;
import com.abc.xxx.model.SysConfig;
import com.abc.xxx.service.SysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static java.util.stream.Collectors.toMap;
import static org.apache.commons.lang3.StringUtils.isNotBlank;

@Slf4j
@Service
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements SysConfigService {

    /**
     * 订阅模块
     */
    private static final int SUBSCRIBER_MODULE = 1;

    /**
     * 采集器模块
     */
    private static final int COLLECTOR_MODULE = 2;


    @Autowired
    private RestTemplate restTemplate;


    @Override
    public Map<String, String> loadAllConfig() {
        List<SysConfig> list = list();
        Map<String, String> map = list.stream().collect(toMap(c -> c.getKeyOrDefaultEmpty(), c -> c.getValOrDefaultEmpty()));
        return map;
    }

    @Override
    public String loadConfigByKey(String key) {
        QueryWrapper<SysConfig> q = new QueryWrapper<>();
        q.select("`id`,`val`");
        q.eq("`key`", key);
        List<SysConfig> list = list(q);
        if (list != null && !list.isEmpty()) {
            return list.get(0).getVal();
        }
        return "";
    }

    @Override
    public void update(String key, String val) {
        Objects.requireNonNull(key, "配置key不能为空");
        Objects.requireNonNull(val, "配置val不能为空");

        SysConfig conf = new SysConfig();
        conf.setVal(val);
        conf.setUpdated(LocalDateTime.now());
        conf.setVal(val);

        QueryWrapper<SysConfig> q = new QueryWrapper<>();
        q.eq("`key`", key);
        update(conf, q);
    }

    @Override
    public void updateWithProfile(String profile, String key, String val) {
        Objects.requireNonNull(profile, "配置profile不能为空");
        Objects.requireNonNull(key, "配置key不能为空");
        Objects.requireNonNull(val, "配置val不能为空");

        SysConfig conf = new SysConfig();
        conf.setVal(val);
        conf.setUpdated(LocalDateTime.now());
        conf.setVal(val);

        QueryWrapper<SysConfig> q = new QueryWrapper<>();
        q.eq("`profile`", profile);
        q.eq("`key`", key);
        update(conf, q);
    }

    @Override
    public List<SysConfig> findSysConfigs(String key) {
        QueryWrapper<SysConfig> q = new QueryWrapper<>();
        if (isNotBlank(key)) {
            q.like("`key`", key);
        }
        q.orderByAsc("`key`");
        return list(q);
    }

    @Override
    public boolean isKeyExists(String key) {
        QueryWrapper<SysConfig> q = new QueryWrapper<>();
        q.eq("`key`", key);
        return retBool(count(q));
    }

    @Override
    public boolean isUpdateTargetKeyExists(Integer sysConfigId, String key) {
        QueryWrapper<SysConfig> q = new QueryWrapper<>();
        q.ne("`id`", sysConfigId);
        q.eq("`key`", key);
        return retBool(count(q));
    }

    /**
     * 通过post请求发送json
     *
     * @param url
     * @param reqBody
     * @return
     */
    private String postJson(String url, String reqBody) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("content-type", "application/json");
        HttpEntity<String> reqEntity = new HttpEntity<>(reqBody, headers);
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, reqEntity, String.class);
        return response.getBody();
    }


}
